Using Beatnik and Flash Together

  Contents

Why Use Beatnik and Flash Together ?

The Beatnik and Flash technologies are ideal partners for creating high-impact, low-bandwidth, multimedia-rich Web content. In fact, Flash and Beatnik are quite similar in concept - both use an approach to data that results in economical, small file sizes - which, in turn, translate to shorter download times and less waiting for the end-user. With Flash, a vector-based approach to graphics not only results in small file sizes, but it opens up a whole world of opportunities for real-time control and animation. With Beatnik, a structured audio approach to music not only creates music files that are extremely more economical than alternate formats (including streaming formats), but it also opens up immense opportunities for real-time control of the music.

Beatnik and Flash are in fact so similar in principle, they could almost be regarded as cousins. Best of all, both Beatnik and Flash are friendly to external scripting control, so it is possible to tightly integrate Flash movies and Beatnik music on the same HTML page, with Flash movie events being sonified by Beatnik, and Beatnik musical events driving Flash movies, and both Beatnik and Flash responding to user interaction or being synchronized to DHTML actions using JavaScript.

Controlling Beatnik from Flash

One of the most typical ways to use Beatnik and Flash together is to sonify Flash movies by controlling Beatnik using Flash FSCommand actions. These actions can be added at key positions in a Flash movie, or can be triggered from user interactions with a Flash movie. In this documentation we will not go into details about how to author Flash movies, but will assume that you already have a background in creating Flash movies. We will also not go into details about the sonification possibilities of the Music Object API, since all the principles that are explained in this documentation that relate to Web page sonification are also applicable to sonifying Flash movies.

NOTE TO FLASH DEVELOPERS :
If you are approaching this section of the documentation with only a background in Flash movie authoring, then you should first familiarize yourself with the principles of sonification using the Music Object API. Once you know more about Music Object sonification (and understand the relationship between Music Object and Beatnik), then you can then come back to this section. In fact, it is best if you have even done some basic sonification exercises using plain Web pages and the Music Object API. This will give you a firm grounding in the sonification principles you will be using when you sonify Flash movies with Beatnik.

Now, when Beatnik is used on a Web page it is possible to control Beatnik's interactive music and audio capabilities from any events in the page - regardless of whether the events are triggered from HTML elements or from within Flash movies. Both Netscape Navigator and Microsoft Internet Explorer offer ways to communicate between browser add-on components and the browser's scripting layer, so it is possible to sonify Flash movies in a way that will work in both browsers. Beatnik and Flash can effectively "talk" to each other through the language of JavaScript.

A Beatnik-Flash Sonification Template

In order to be able to execute Music Object sonification instructions from within a Flash movie, it is necessary to first add some "wrapper" code into your page to support the Flash FSCommand action. A template for this is provided below...

Copy-and-Paste
<HTML><HEAD><TITLE>Beatnik and Flash Template</TITLE></HEAD>
<BODY>

<SCRIPT SRC="correct-path-here/music-object.js"></SCRIPT>

<OBJECT
   ID="myFlashMovie"
   WIDTH=600
   HEIGHT=400
   ALIGN=TOP
   CLASSID="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
   CODEBASE="http://active.macromedia.com/flash/cabs/swflash.cab"
>
   <PARAM NAME="movie" VALUE="my-flash-movie.swf">
   <PARAM NAME="quality" VALUE="high">
   <EMBED
      SRC="my-flash-movie.swf"
      WIDTH=600
      HEIGHT=400
      ALIGN=TOP
      NAME="myFlashMovie"
      QUALITY="high"
      BASE="http://www.mydomain.com"
      SWLIVECONNECT=TRUE
      MAYSCRIPT
      TYPE="application/x-shockwave-flash"
      PLUGINSPAGE="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"
   >
</OBJECT>

<!----------- BEGIN FSCommand Wrapper Code ----------->

<SCRIPT LANGUAGE="JavaScript"><!--

function myFlashMovie_DoFSCommand (command,args) {
   eval (command + ((command.indexOf ('(') == -1) ? ('(' + args + ')') : ''));
}

//--></SCRIPT>

<SCRIPT LANGUAGE="VBScript"><!--

Sub myFlashMovie_FSCommand (ByVal command, ByVal args)
        call myFlashMovie_DoFSCommand (command, args)
end sub

//--></SCRIPT>

<!------------ END FSCommand Wrapper Code ------------>

<SCRIPT LANGUAGE=JavaScript><!-- //

player1 = new Music ();
player1.magicEmbed ('SRC="my-music-file.rmf" WIDTH=0 HEIGHT=0 HIDDEN AUTOSTART=TRUE');

// --></SCRIPT>

</BODY></HTML>

In this template, the highlighted text represents the parts that should be customized to suit the needs of your specific application. These parts are critically important to making the Flash-to-Beatnik wiring work correctly. You should pay close attention to matching up all the things that need to be matched up. For example, all occurrences of "myFlashMovie" in this template should be substituted with the name that you choose for the Flash movie instance. If you don't change all the occurrences to exactly the same name, then the page is almost certain to not work correctly. The same applies to "player1" and "my-flash-movie.swf".

In this template we are creating one Flash movie instance using the OBJECT / EMBED tag combination, and we are creating one Beatnik Player instance using Music Object's magicEmbed method. You should also note the block of wrapper code that supports the Flash FSCommand action that allows Flash to control Beatnik with JavaScript instructions. You will see that this wrapper code is "stamped" with the name of the Flash movie instance that is to be sonified. This means that each sonified Flash movie on a Web page must have a unique block of FSCommand wrapper code like this.

Adding FSCommand Actions

Now that the Web page has been set up for Flash-to-Beatnik communication, all one needs to do is add FSCommand actions inside the Flash movie to trigger sonification events inside the HTML page. To take a very simple example, let's assume that an RMF music file is playing through Beatnik and, at a certain point in the Flash movie, it is to fade out to a stop. This could be done by simply adding the statement player1.stop (true) into the Flash movie as an FSCommand action, as shown in the screenshot below...

Adding an FSCommand Action

Using "Music Object Extension - Flash"

To simplify the process of wiring Flash movies to use the FSCommand, Beatnik Inc. has provided the convenient Music Object Extension - Flash module. Let's take a look at how the Music.embedFlashMovie static method provided in this Music Object extension can simplify the Beatnik-Flash sonification template shown earlier. Compare the following revised Beatnik-Flash sonification template to the earlier template...

Copy-and-Paste
<HTML><HEAD><TITLE>Beatnik-Flash Template Using Music.embedFlashMovie</TITLE></HEAD>
<BODY>

<SCRIPT SRC="correct-path-here/music-object.js"></SCRIPT>
<SCRIPT SRC="correct-path-here/music-object-x-flash.js"></SCRIPT>

<SCRIPT LANGUAGE=JavaScript><!-- //

Music.embedFlashMovie (
   'SRC="my-flash-movie.swf" NAME="myFlashMovie" BASE="http://www.mydomain.com" ' +
   'WIDTH=600 HEIGHT=400 ALIGN=TOP QUALITY="high"'
);
player1 = new Music ();
player1.magicEmbed ('SRC="my-music-file.rmf" WIDTH=0 HEIGHT=0 HIDDEN AUTOSTART=TRUE');

// --></SCRIPT>

</BODY></HTML>

As you can see, by using the Music.embedFlashMovie method, the number of places has been reduced where you have to make sure values exactly match up, and the code is also far simpler, cleaner, and easier to update.

Springboard to a World of Sonification Possibilities

Understanding this very basic principle of "wiring" Flash to Beatnik, there are so many possibilities for interactive music / audio that are opened up. You only need to visit the Beatnik Showcase to get an idea of the many cool ways one could use Beatnik together with Flash. To reiterate, all the principles that apply to sonifying Web page HTML elements using Beatnik apply as well to sonifying events within Flash movies.